<template>
  <div class="weekly_plan_manage_planner">
    <one-title title="周计划管理" @refreshData="refresh">
      <div class="mes_btn">
        <el-tooltip
          effect="dark"
          :content="returnBtnStatus.content"
          :disabled="!returnBtnStatus.status"
          popper-class="btn_tooltip"
          placement="bottom">
          <el-button
            v-btn-permission="'return'"
            @click="handleReturn" :class="{'is-disabled':returnBtnStatus.status}">
            <i class="iconfont icon-btn_return"></i>
            退回
          </el-button>
        </el-tooltip>
        <el-tooltip
          effect="dark"
          :content="submitBtnStatus.content"
          :disabled="!submitBtnStatus.status"
          popper-class="btn_tooltip"
          placement="bottom">
          <el-button
            v-btn-permission="'confirm'"
            type="success" @click="handleSubmit" :class="{'is-disabled':submitBtnStatus.status}">
            <i class="iconfont icon-btn_Save"></i>
            确认
          </el-button>
        </el-tooltip>
      </div>
    </one-title>
    <div class="fixed_top_56">
      <mes-search-comp
        ref="mesSearch"
        @searchInfo="_info"
        :searchList="tableColConfig"
        :searchResult="searchCount">
      </mes-search-comp>
      <div class="table_container">
        <el-table
          :data="listData.list"
          ref="tableNode"
          v-loading="isLoading"
          :max-height="tableMaxHeight"
          :row-class-name="({row}) => !selectItems.some((item) => item.id === row.id) || 'line_selected'"
          @row-click="(row) => $refs.tableNode.toggleRowSelection(row)"
          @sort-change="handleSortChange"
          @selection-change="handleSelectionChange"
          border
          stripe
          tooltip-effect="dark">
          <el-table-column
            width="40"
            type="selection"
            align="center">
          </el-table-column>
          <mes-col-comp
            v-for="(column,index) in columnList"
            :key="index"
            :column="column">
          </mes-col-comp>
          <el-table-column
            label="操作"
            show-overflow-tooltip
            :resizable="false"
            align="center"
            fixed="right"
            min-width="80">
            <template slot-scope="scope">
              <div class="operation">
                <el-tooltip class="item" effect="dark" content="操作记录" :open-delay="openDelay" placement="bottom">
                  <el-button type="text" size="small">
                    <i class="iconfont icon-td_check" @click.stop="handleViewLog(scope.row)"></i>
                  </el-button>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="下载" :open-delay="openDelay" placement="bottom">
                  <el-button type="text" size="small">
                    <i class="iconfont icon-download"
                       @click.stop="$downByBlob(scope.row.uploadHistoryId, scope.row.fileName)"></i>
                  </el-button>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="预览" :open-delay="openDelay" placement="bottom">
                  <el-button type="text" size="small">
                    <i @click.stop="$previewFile(scope.row.uploadHistoryId)"
                       v-if="$checkPreviewName(scope.row.fileName)&&$checkPreviewSize(scope.row.fileSize)"
                       class="iconfont icon-chakanyulan"></i>
                    <i v-else class="iconfont icon-chakanyulan disicon"></i>
                  </el-button>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <mes-pagination
        @updateData="init"
        :currentPage="listData.pageNo"
        :total="listData.count || 0"
        :pageSize="listData.pageSize">
      </mes-pagination>
    </div>
    <!-- 日志详情 -->
    <log-detail ref="logDetail"></log-detail>
  </div>
</template>

<script>
import FileIcon from '@/components/FileIcon'
import {WeekPlanStatus} from '@/components/mesDict'
import tableColConfig from './tableColConfig'

export default {
  name: 'Auditor',
  components: {
    FileIcon,
    WeekPlanStatus
  },
  data() {
    return {
      isLoading: true, // 数据加载中
      searchData: {}, // 查询参数
      sortData: {}, // 排序对象
      searchCount: NaN, // 搜索结果数
      selectItemsDialog: [], // 选中弹框中的数据,
      selectItems: [], // 选中的数据,
      uploadHistoryId: '', // 上传文件的历史id
      dialogViewLog: false, // 查看日志
      ViewLogName: '', // 查看日志上显示的周计划名称
      weekplanList: []
    }
  },
  mounted() {
    this.init()
  },
  computed: {
    tableColConfig: tableColConfig, //列配置
    // 列表数据
    listData() {
      return this.$store.state.auditor.listDataAuditor
    },
    logList() {
      return this.$store.state.auditor.logList
    },
    // 周计划状态原始列表
    WeekPlanStatus() {
      return this.$store.state.dict.WeekPlanStatus.labels || []
    },
    // 周计划列表
    WeekPlanStatusList() {
      return [{
        id: '',
        label: '全部'
      }, ...this.$store.getters.filterDictByValues('WeekPlanStatus', ['Unconfirmed', 'Confirmed'])]
      // let list = this.WeekPlanStatus.filter((item) => item.value !== Draft && item.value !== Dismissed)
      // return list.length > 0 ? [{id: '', label: '全部'}, ...list] : list
    },
    // 列循环
    columnList() {
      return this.tableColConfig.filter(item => item.noForCol !== true && item.isShow !== false)
    },
    // 退回按钮
    returnBtnStatus() {
      let status = false
      if (this.selectItems.some((item) => item.weekPlanStatusValue !== 'Unconfirmed')) {
        status = true
      }
      return {
        status: status,
        content: '请选择状态是"待确认"的数据'
      }
    },
    submitBtnStatus() {
      let status = false
      if (this.selectItems.some((item) => item.weekPlanStatusValue !== 'Unconfirmed')) {
        status = true
      }
      return {
        status: status,
        content: '请选择状态是"待确认"的数据'
      }
    }
    // 确认按钮
  },
  methods: {
    // 初始化方法
    init(page) {
      this.isLoading = true
      let postData = {
        pageNo: page ? page.pageNo : 1,
        pageSize: page ? page.pageSize : this.listData.pageSize || 15,
      }
      let WeekPlanIds = this.WeekPlanStatusList[1].id + ',' + this.WeekPlanStatusList[2].id
      if (Object.keys(this.searchData).length > 0) {
        postData.query = this.searchData
        if (!postData.query.weekPlanStatusId) {
          postData.query.weekPlanStatusId = WeekPlanIds
        }
      } else {
        postData.query = {weekPlanStatusId: WeekPlanIds}
      }
      if (Object.keys(this.sortData).length > 0) {
        postData.sort = this.sortData
      }
      this.$store.dispatch('saveData', {
        method: 'post',
        url: 'a/weekplan/weekPlan/list',
        data: postData,
        module: 'auditor',
        state: 'listDataAuditor'
      }).then((res) => {
        if (Object.keys(this.searchData).length > 0) {
          this.searchResult = res.count
        } else {
          this.searchResult = NaN
        }
      }).finally(() => {
        this.isLoading = false
      })
    },
    // 搜索方法
    _info(data) {
      this.searchData = data
      this.init()
    },
    // 排序方法
    handleSortChange(e) {
      this.sortData = this.$getSortOrder(e)
      this.init()
    },
    // 刷新方法
    refresh() {
      this.searchData = {}
      this.$refs.mesSearch.clean()
      this.$refs.tableNode.clearSort()
      this.init()
    },
    // 选中方法
    handleSelectionChange(val) {
      this.selectItems = val
    },
    // 可选项过滤
    // optional(row) {
    //   switch (row.weekPlanStatusValue) {
    //     case Unconfirmed:
    //       return true
    //     default:
    //       return false
    //   }
    // },
    // 退回方法
    handleReturn() {
      if (!this.selectItems.length) {
        return this.tipMessage('error', '请选择一条周计划数据')
      }
      if (this.returnBtnStatus.status) return
      this.mesConfirm('选中的记录将被退回, 是否继续?', '退回提示').then(() => {
        let postData = {
          method: 'post',
          url: 'a/weekplan/weekPlan/changePlanStatus',
          data: {
            changeType: 'Dismissed',
            weekplanList: this.selectItems
          },
          noCommit: true
        }
        this.$store.dispatch('saveData', postData).then(() => {
          this.tipMessage('success', '退回成功')
          this.dialogVisible = false
          this.init(this.listData)
        })
      })
    },
    // 上传文件
    uploadFile(e) {
      let file = e.file
      this.fileName = file.name
      let formData = new FormData()
      formData.append('file', file)
      this.$store.dispatch('importFileSave', {
        data: formData
      }).then(res => {
        // 上传的文件
        this.uploadHistoryId = res.id
      })
    },
    // 确认
    handleSubmit() {
      if (!this.selectItems.length) {
        return this.tipMessage('error', '请选择一条周计划数据')
      }
      if (this.submitBtnStatus.status) return
      this.mesConfirm('选中的记录将被确认, 是否继续?', '确认提示').then(() => {
        let postData = {
          method: 'post',
          url: 'a/weekplan/weekPlan/changePlanStatus',
          data: {
            changeType: 'Confirmed',
            weekplanList: this.selectItems
          },
          noCommit: true
        }
        this.$store.dispatch('saveData', postData).then(() => {
          this.tipMessage('success', '确认成功')
          this.dialogVisible = false
          this.init(this.listData)
        })
      })
    },
    // 查看日志
    handleViewLog(row) {
      this.$refs.logDetail.openDialog({
        title: '周计划名称',
        name: row.name,
        url: 'a/weekplanstatuschangehistory/weekPlanStatusChangeHistory/list',
        query: {
          weekPlanId: row.id
        }
      })
    },
    /**
     * 提示
     * @param str 提示内容
     */
    tipMessage(type, str) {
      this.$message({
        type: type,
        message: str,
        duration: 1500,
        center: true
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .weekly_plan_manage_planner {
    .el-upload.el-upload--text span {
      display: inline-block;
      width: 200px;
      text-align: left;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      vertical-align: middle;
    }

    .operation {
      color: #00b4ff;
      display: flex;
      justify-content: space-around;

      .iconfont {
        cursor: pointer;

        &.disicon {
          cursor: not-allowed;
          color: #a1b1c1;
        }
      }
    }

    .viewName {
      margin-bottom: 10px;
    }
  }
</style>
